<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends BaseController {
      

    public function sms_valid_code()
    {  
        $sys=M('sys')->where(array('sys_id'=>1))->find();
        $user=$sys['sms_user'];
        $pass=md5($sys['sms_pwd']);
        $mobile=I('post.m', '', 'htmlspecialchars');
      $t=I('post.t', '', 'htmlspecialchars');
      $c=rand(100000,999999);
     session('valid_code',$c);
      if (isTelNumber($mobile)) {
        $content='您的验证码为'.$c;
         $res=sendmobile($mobile,$content,$user,$pass);
         if ($res='短信发送成功') {
             exit('2');
         }
        
      }else{
        exit('-1');
      }

    }


    public function modify_pass()
    {
      $map['password']=md5(I('post.old_password'));
      $map['id']=I('post.id');
      $res=M('user')->where($map)->find();
      if ($res) {
        $data['password']=md5(I('post.new_password'));
        $data['id']=I('post.id');
        M('user')->save($data);
        $this->success('密码修改成功');
      }else{
        $this->error('旧密码不正确');
      }
    }


     public function modify_info()
    {
      $map['id']  = array('neq',I('post.id'));
      $map['idcard']=I('post.idcard');
      $map2['id']  = array('neq',I('post.id'));
      $map2['mobile']=I('post.mobile');
      $res=M('user')->where($map)->find();
      $res2=M('user')->where($map2)->find();
      if ($res) {
        $this->error('身份证号已经占用');exit();
      }

      if ($res2) {
        $this->error('手机号已经占用');exit();
      }

M('user')->save($_POST);
     $this->success('修改信息成功');
    }


    //积分转让
    public function jfzr()
    {   
        $userid = session('user.id');
        $zr_userid = I('zr_user_id');
        $cfjf=I('zr_cfjf_num');
        

        $record['jifen_type']='cf';
        $record['jifen_num']='-'.$cfjf;
        $record['addtime']=time();
        $record['user_id']=$userid;
        $record['items']='积分转让';
        $record['zr_desc']=I('zr_desc');
        $record['zr_userid']=$zr_userid;
        $record['explain']='转给了您邀请的好友，id：'.$zr_userid;
        
        $record2['jifen_type']='cf';
        $record2['jifen_num']='+'.$cfjf;
        $record2['addtime']=time();
        $record2['user_id']=$zr_userid;
        $record2['items']='积分转让';
        $record2['zr_desc']=I('zr_desc');
        $record2['from_userid']=$userid;
        $record2['explain']='您的服务人，id：'.$zr_userid.'转给了您财富积分';
          M()->startTrans();
         $res1=M('jifen_record')->add($record);
         $res2=M('jifen_record')->add($record2);

         $res3=M('user')->where(array('id'=>$userid))->setDec("cf_jifen",$cfjf);
         $res4=M('user')->where(array('id'=>$zr_userid))->setInc("cf_jifen",$cfjf);

          if ($res2 && $res1 && $res3 && $res4) {

              M()->commit(); 
            $this->success('转让成功'); 
          }else{
            M()->rollback();
          }

    }


    public function addOption()
    {
      $data=$_POST;
      $data['user_id']=session('user.id');
      $data['add_time']=time();
      $res=M('options')->add($data);
      if ($res) {
        $this->success('感谢您的反馈');
      }
    }

    public function tixian()
    {
      
      $data=$_POST;
      $data['add_time']=time();
      
      $tixian_fee=M('sys')->getField('tixian_fee');
$data['final_tixian']=$data['cf_tixian']*(1-$tixian_fee/100);
$data['tixian_fee']=$tixian_fee;
     M()->startTrans();
$map['id']=session('user.id');
     $res1=M('tixian')->add($data);
     $res2=M('user')->where($map)->setDec('cf_jifen',$data['cf_tixian']);
     $res3=M('user')->where($map)->setInc('cf_frozen',$data['cf_tixian']);

     if ($res1 && $res2 && $res3 ) {
         M()->commit(); 

         $tixian_explain=M('sys')->getField('tixian_explain');
         $info= '提现申请成功'.$tixian_explain;
         $this->success($info);
     }else{
        M()->rollback();
     }
    }









 }
